package session;

import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lowagie.text.pdf.PRTokeniser;

@Stateless
public class GenericSession<T> {

	@PersistenceContext
	protected EntityManager em;
	
	protected Class<T> t;
	
	
	public GenericSession(Class<T> clazz){
		t = clazz;
	}
	@TransactionAttribute
	public void insert(T t){
		em.persist(t);
	}
	@TransactionAttribute
	public void update(T t){
		em.merge(t);
	}
	@TransactionAttribute
	public void delete(T t){
		em.remove(em.merge(t));
	}
	
	@TransactionAttribute
	public T findById(Integer id){
		return em.find(t, id);
	}
	@TransactionAttribute
	@SuppressWarnings("unchecked")
	public List<T> findAll() {
		String query = "from ";
		query += t.getSimpleName()+" t";
		return em.createQuery(query).getResultList();
	}

}
